<template>
  <div>
    <h1>停车场管理系统</h1>
    <img src="../assets/imgs/shark.png">
    <h2>WeiLaiParking</h2>
    <el-card style="width: 600px;height: 200px;margin: 0 auto;
    background-color: rgba(255,255,255,0.3)">

      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" ></el-input>
        </el-form-item>
        <el-form-item style="position: relative;right: 25px">
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>
<style>
body{
  text-align: center;
  background-image: url("../assets/imgs/background.jpg");
  margin: 0;/*去掉自带外边距*/
  background-size: cover;/*cove  把背景图片设置为封面  设置之后  背景图片 不会随着浏览器缩放而缩放 会自动填充 */
  background-repeat: no-repeat;/*背景图片不重复*/
}
h1{
  font-size: 72px;
  color: #0096dc;
  margin-bottom: 0;
}
h2 {
  font-size: 32px;
  color: #0096dc;
  margin-bottom: 0;
}
</style>
<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4 到 15 个字符', trigger: 'blur' }
        ],

      }

    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        let url ='http://localhost:8080/admin/login'
        console.log(this.ruleForm)
       this.axios
           .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
           .post(url,this.ruleForm).then((response)=>{
          let  responseBody = response.data;
          let jwt = responseBody.data;
          console.log(jwt);
          localStorage.setItem('jwt',jwt);
         if (responseBody.code==200){
           this.$message({
             showClose: true,
             message: '登录成功！',
             type: 'success'
           });
           this.$router.push('/admin');
         }else if (responseBody.code == 404){
           this.$message({
             showClose: true,
             message: responseBody.message,
             type: 'error'
           });
         }else if (responseBody.code==400){
           this.$message({
             showClose: true,
             message: responseBody.message,
             type: 'error'
           });
         }
       })
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}

</script>
